package com.wisdudu.blank.support.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.wisdudu.frame.support.util.DensityUtil;

import java.util.List;

/**
 * Created by pc on 2016/3/8.
 */
public class DiagramView extends View {
    private Context myContext;
    public int paddingLeft;
    public int paddingRight;
    public int paddingBottom;
    public int paddintTop;

    List<Double> listDate1;  //数据源
    double maxDate;  //集合最大值,占满整个高度
    int jgwidth; //2点间隔宽度
    public int index;  //当前选中的index;
    public int maxI;  // 最高 数值的  位置；
    List<String> scoreList;
    List<String> timeList;
    private int px35;
    private String date;

    int[] x;
    int[] y;

    static boolean isFist = true;//第一次进来,用于判断点显示不显示要用的
    boolean isInit = false;      //是否已经传过来初始化所有参数,没有的话,view不显示,ondraw里面判断


    public void init() {
        isFist = true;
        isInit = false;
    }

    public DiagramView(Context context) {
        super(context);
        this.myContext = context;
    }

    public DiagramView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.myContext = context;
    }

    public DiagramView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.myContext = context;

    }


    public void setInit(List<Double> listDate1, List<String> listString1, List<String> listString2) {
        this.listDate1 = listDate1;
        this.scoreList = listString1;
        this.timeList = listString2;
        isInit = true;
        initDatas();
        invalidate();
        px35 = DensityUtil.dp2px(myContext, 35);
    }

    public void setInit(List<Double> listDate1, List<String> scoreList, List<String> timeList, String date) {
        this.listDate1 = listDate1;
        this.scoreList = scoreList;
        this.timeList = timeList;
        isInit = true;
        initDatas();
        invalidate();
        px35 = DensityUtil.dp2px(myContext, 35);
        this.date = date;
    }

    public void clearData() {
        this.listDate1.clear();
        this.scoreList.clear();
        this.timeList.clear();
    }

    public void setdate(String date) {
        this.date = date;
    }

    public static enum Type {
        week, month
    }

    /**
     * 初始化数据
     */
    public void initDatas() {
        maxDate = getMaxint() == 0 ? 100 : getMaxint();  //算出最大的数
        paddingLeft = DensityUtil.dp2px(myContext, 7);
        paddingRight = DensityUtil.dp2px(myContext, 7);
        paddingBottom = 100;
        paddintTop = DensityUtil.dp2px(myContext, 40);
        x = new int[listDate1.size()];
        y = new int[listDate1.size()];

        jgwidth = (getWidth() - paddingLeft - paddingRight) / (listDate1.size()) + 3;
        //算出来个坐标点
        for (int i = 0; i < listDate1.size(); i++) {
            double kuan = getMyWeight(i);
            double gao = getMyHeight(listDate1.get(i));
            x[i] = (int) kuan;
            y[i] = (int) gao;
        }


        for (int i = 0; i < listDate1.size(); i++) {
            if (maxDate == listDate1.get(i)) {
                index = i;
                maxI = i;
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isInit) {
            return;
        }
        if (isFist) {
            initDatas();
            isFist = false;
        }
        //背景色
//        canvas.drawColor(Color.parseColor("#0049acd5"));

        //绘制任意多边形

        /**
         * 画  白色连线 折线 图
         */
        Paint paint = new Paint();

        //设置画笔颜色
        paint.setColor(Color.parseColor("#ffffff"));
        //设置圆弧的宽度
        paint.setStrokeWidth(DensityUtil.dp2px(myContext, 1));
        //设置圆弧为空心
        paint.setStyle(Paint.Style.FILL);
        //消除锯齿
        paint.setAntiAlias(true);
        for (int i = 0; i < listDate1.size(); i++) {
            double kuan = getMyWeight(i);
            double gao = getMyHeight(listDate1.get(i));
            if (i != 0) {
                double kuan1 = getMyWeight(i - 1);
                double gao1 = getMyHeight(listDate1.get(i - 1));
                canvas.drawLine((float) kuan1 + 5, (float) gao1, (float) kuan - 5, (float) gao, paint);
            }
        }

        /**
         * 画 折线上  所有的点，圆环形式
         */
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.parseColor("#ffffff"));
        //设置圆弧的宽度
        paint.setStrokeWidth(4);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.STROKE);
        //消除锯齿
        paint.setAntiAlias(true);
        for (int item = 0; item < listDate1.size(); item++) {
            //定义一个矩形
            RectF rf2 = new RectF(x[item] - 7, y[item] - 7, x[item] + 7, y[item] + 7);
            rf2.offset(0, 0);
            //画圆
            canvas.drawOval(rf2, paint);
        }


        /**
         * 选中小圆点外部 淡色光晕
         */
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.parseColor("#4FFFFFFF"));
        //设置圆弧的宽度
        paint.setStrokeWidth(1);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.FILL);
        //消除锯齿
        paint.setAntiAlias(true);
        for (int item = 0; item < listDate1.size(); item++) {
            if (index == item) {
                RectF rf2 = new RectF(x[item] - 15, y[item] - 15, x[item] + 15, y[item] + 15);
                rf2.offset(0, 0);
                //画圆
                canvas.drawOval(rf2, paint);
            }
        }


        /**
         * 选中小圆点内部白色
         */
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.parseColor("#FFFFFF"));
        //设置圆弧的宽度
        paint.setStrokeWidth(1);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.FILL);
        //消除锯齿
        paint.setAntiAlias(true);
        for (int item = 0; item < listDate1.size(); item++) {
            if (index == item) {
                RectF rf2 = new RectF(x[item] - 7, y[item] - 7, x[item] + 7, y[item] + 7);
                rf2.offset(0, 0);
                //画圆
                canvas.drawOval(rf2, paint);
            }
        }


        //画 选中的  点的  直线 白色
        /**
         */
        if (getMaxint() > 0) {
            paint = new Paint();
            paint.setColor(Color.parseColor("#FFFFFF"));
            paint.setStrokeWidth(DensityUtil.dp2px(myContext, 2));
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            for (int item = 0; item < listDate1.size(); item++) {
                if (index == item) {
                    double kuan = getMyWeight(item);
                    double gao = getMyHeight(listDate1.get(item));
                    canvas.drawLine((float) kuan, (float) gao + 15, (float) kuan, getHeight() - px35, paint);
                }
            }

            //画 所有的  点的  直线  30%透明  白色
            paint = new Paint();
            paint.setColor(Color.parseColor("#4DFFFFFF"));
            paint.setStrokeWidth(DensityUtil.dp2px(myContext, 1));
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            for (int i = 0; i < listDate1.size(); i++) {
                double kuan = getMyWeight(i);
                double gao = getMyHeight(listDate1.get(i));
                canvas.drawLine((float) kuan, (float) gao + 15, (float) kuan, getHeight() - px35, paint);
            }
        }


        //绘制文字
        paint = new Paint();
        paint.setFakeBoldText(false); //true为粗体，false为非粗体
        //mp.setTextSkewX(-0.5f); //float类型参数，负数表示右斜，整数左斜
        //mp.setUnderlineText(true); //true为下划线，false为非下划线
        //mp.setStrikeThruText(true); //true为删除线，false为非删除线
        paint.setTextSize(DensityUtil.sp2px(myContext, 10)); //设置字体大小，int型，如12
        paint.setShader(null);
        paint.setStrokeWidth(10);
        paint.setColor(Color.WHITE);
        // 画  选中 的  分数
        for (int i = 0; i < x.length; i++) {
            if (index == i) {
                if (i == 0) {
                    canvas.drawText(scoreList.get(i) + "分", x[i], y[i] - 30, paint);
                } else if (i == listDate1.size() - 1) {
                    Log.e("-->", "分，进来了");
                    canvas.drawText(scoreList.get(i) + "分", x[i] - 30 - 50, y[i] - 30, paint);
                } else {
                    canvas.drawText(scoreList.get(i) + "分", x[i] - 30, y[i] - 30, paint);
                }

            }
        }

        //画  底部  时间
        for (int i = 0; i < x.length; i++) {
            canvas.drawText(timeList.get(i), x[i] - 20, getHeight() - DensityUtil.dp2px(myContext, 15), paint);
        }

        paint.setTextSize(DensityUtil.sp2px(myContext, 14)); //设置字体大小，int型，如12
        // 画 左上角  日期
        if (!TextUtils.isEmpty(date)) {
            double height = 0;
            if (maxI != 0) {
                height = getMyHeight(listDate1.get(maxI)) - DensityUtil.dp2px(myContext, 20);
            } else {
                height = 80;
            }
            canvas.drawText(date, x[0] + DensityUtil.dp2px(myContext, 20),
                    (float) height, paint);
        }
    }


    public double getMaxint() {
        double max = 0;
        for (int i = 0; i < listDate1.size(); i++) {
            if (listDate1.get(i) > max) {
                max = listDate1.get(i);
            }
        }
        return max;
    }

    //穿入数据源返回数据源应有的高度
    public double getMyHeight(double d) {
        int height = getHeight() - paddintTop - paddingBottom;
        return (height - (height * (d / maxDate))) + paddintTop;
    }


    public double getMyWeight(int posotion) {
        return posotion * jgwidth + paddingLeft + paddingRight;
    }


    boolean pd1 = false;
    boolean pd2 = false;
    float mx1, mx2, my1, my2;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:  //按下 = 0
                mx1 = event.getX();
                my1 = event.getY();
                if (pd1) {
                    pd1 = false;
                }
                pd1 = true;
                Log.e("-->", "按下");
                break;
            case MotionEvent.ACTION_MOVE:  //移动 = 2
                pd2 = true;
                Log.e("-->", "移动");
                break;
            case MotionEvent.ACTION_UP:    // 抬起 = 1
                mx2 = event.getX();
                my2 = event.getY();
                float number = mx1 - mx2 > 0 ? mx1 - mx2 : mx2 - mx1;
                if (pd1 & pd2 & number > 10) {
                    if (number > 10) {
                        Log.e("-->", "滑动");
                        if ((mx1 - mx2) > 0) {
                            Log.e("-->", "往左边滑动");
                            index = index > 0 ? index - 1 : index;
                        } else {
                            Log.e("-->", "往右边滑动");
                            index = index < listDate1.size() - 1 ? index + 1 : index;
                        }
                        invalidate();
                    }
                } else {
                    int item = isok(event.getX(), event.getY());
                    index = item == -1 ? index : item;
                    invalidate();
                    Log.e("-->", "抬起");
                }
                break;
        }

        //gestureDetector.onTouchEvent(event);
        return true;
    }

    //判断点和矩形是否相交
    public boolean isXiangjiao(float dx, float dy, int juBeginX, int juBeginY, int juEndX, int juEndY) {
        if (dx >= juBeginX & dy >= juBeginY & dx <= juEndX & dy <= juEndY) {
            return true;
        } else {
            return false;
        }
    }

    //判断点击的坐标是否在个圆圈里面,是则返回圈的下标,不是返回-1
    public int isok(float djx, float djy) {
        for (int i = 0; i < x.length; i++) {
            if (isXiangjiao(djx, djy, x[i] - 60, y[i] - 90, x[i] + 60, y[i] + 60)) {
                return i;
            }
        }
        return -1;
    }

}
